package cst.a720yyq.zyfypt_720yyq;


import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a720yyq.zyfypt_720yyq.Bean.VideoBean;
import cst.a720yyq.zyfypt_720yyq.Interface.ToudouListener;
import cst.a720yyq.zyfypt_720yyq.Modle.ResModle;
import cst.a720yyq.zyfypt_720yyq.View.Fragment.ArticalFragment;
import cst.a720yyq.zyfypt_720yyq.View.Fragment.TwareFragment;
import cst.a720yyq.zyfypt_720yyq.View.Fragment.VideoFragment.PSVideoFragment;
import cst.a720yyq.zyfypt_720yyq.View.Fragment.Case_ProjFragment.CaseFragment;
import cst.a720yyq.zyfypt_720yyq.View.Fragment.MyFragment;
import cst.a720yyq.zyfypt_720yyq.View.toasty.Toasty;

public class MainActivity extends AppCompatActivity {
    private ResModle modle;
    private ToudouListener<List<VideoBean>> listener;
    private LinearLayout layout1,layout2,layout3,layout4,layout5;
    private ImageButton ib1,ib2,ib3,ib4,ib5;
    private ViewPager vp;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_main);
        init();
        initEvents();
        initFragment();
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        modle = new ResModle("gson");
    }
    private void initFragment() {
        listfragment=new ArrayList<android.support.v4.app.Fragment>();
        listfragment.add(new ArticalFragment());
        listfragment.add(new TwareFragment());
        listfragment.add(new PSVideoFragment());
        listfragment.add(new CaseFragment());
        listfragment.add(new MyFragment());

        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public android.support.v4.app.Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp.setAdapter(fpadapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("----"+String.valueOf(position));
                vp.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    View.OnClickListener  onClickListener =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //先将四个按钮都设成未选图片
            resetImage();
            switch (view.getId())
            {
                case R.id.layout1:
//                    Toast.makeText(MainActivity.this, "weixin", Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.layout2:
//                    Toast.makeText(MainActivity.this, "friend", Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
                case R.id.layout3:
//                    Toast.makeText(MainActivity.this, "address", Toast.LENGTH_SHORT).show();
                    selectTab(2);
                    break;
                case R.id.layout4:
//                    Toast.makeText(MainActivity.this, "setting", Toast.LENGTH_SHORT).show();
                    selectTab(3);
                    break;
                case R.id.layout5:
//                    Toast.makeText(MainActivity.this, "setting", Toast.LENGTH_SHORT).show();
                    selectTab(4);
                    break;
            }
        }
    };
    private  void resetImage()
    {
        ib1.setImageResource(R.drawable.page_normal);
        ib2.setImageResource(R.drawable.kejian_normal);
        ib3.setImageResource(R.drawable.video_normal);
        ib4.setImageResource(R.drawable.anli_normal);
        ib5.setImageResource(R.drawable.me_normal);
    }
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                ib1.setImageResource(R.drawable.page_select);
                break;
            case 1:
                ib2.setImageResource(R.drawable.kejian_select);
                break;
            case 2:
                ib3.setImageResource(R.drawable.video_select);
                break;
            case 3:
                ib4.setImageResource(R.drawable.anli_select);
                break;
            case 4:
                ib5.setImageResource(R.drawable.me_select);
                break;
        }
        vp.setCurrentItem(i);
    }


    private void initEvents() {
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        layout5.setOnClickListener(onClickListener);
    }

    private void init() {
        layout1=(LinearLayout) findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);
        layout5=(LinearLayout)findViewById(R.id.layout5);

        ib1=(ImageButton)findViewById(R.id.imageButton1);
        ib2=(ImageButton)findViewById(R.id.imageButton2);
        ib3=(ImageButton)findViewById(R.id.imageButton3);
        ib4=(ImageButton)findViewById(R.id.imageButton4);
        ib5=(ImageButton)findViewById(R.id.imageButton5);

        vp=(ViewPager)findViewById(R.id.viewPager);
    }
    ////////////////////////////////////////////////////后台运行
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        PackageManager pm = getPackageManager();
        ResolveInfo homeInfo =
                pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ActivityInfo ai = homeInfo.activityInfo;
            Intent startIntent = new Intent(Intent.ACTION_MAIN);
            startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            startIntent.setComponent(new ComponentName(ai.packageName, ai.name));
            startActivitySafely(startIntent);
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }
    private void startActivitySafely(Intent intent) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "null",
                    Toast.LENGTH_SHORT).show();
        } catch (SecurityException e) {
            Toast.makeText(this, "null",
                    Toast.LENGTH_SHORT).show();
        }
    }
////////////////////////////////////////////////////////后台运行

}
